package com.wdcg.utils;

import java.util.List;
import java.util.Random;

import com.wdcg.ApplicationSharedPreferences;
import com.wdcg.R;
import com.wdcg.activity.ARGameActivity;
import com.wdcg.activity.PuzzleGameActivity;
import com.wdcg.activity.QuizActivity;
import com.wdcg.model.access.BonusAccess;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.model.dao.BonusTypeDescription;
import com.wdcg.model.dao.Dwarf;
import com.wdcg.model.dao.DwarfDescription;

import android.content.Context;
import android.content.Intent;
import android.location.Location;


public class GameLogic {

	private static final int BONUS_TYPE_PLUS_ONE_DWARF = 1;
	private static final int BONUS_TYPE_PLUS_CLOSEST_DWARF = 2;
	private static final int BONUS_TYPE_MINUS_ONE_DWARF = 3;
	private static final int BONUS_TYPE_PLUS_N_DWARVES_ONCE = 4;
	private static final int BONUS_TYPE_MINUS_N_DWARVES_ONCE = 5;


	public static void newGame(Context context){


		int numberOfDwarfs = ApplicationSharedPreferences.getNumberOfDwarfs();

		for(int i = 0; i<numberOfDwarfs; i++)
			DwarfAccess.setRandomDwarfAsVisible();

	}

	public static void playMiniGame(Context context, int dwarfId){

		int game = (int) (Math.random()*3);

		if(game==0){
			Intent intent = new Intent(context, QuizActivity.class);
			intent.putExtra("DwarfID", dwarfId);
			context.startActivity(intent);
		}

		else if(game==1){
			Intent intent = new Intent(context, PuzzleGameActivity.class);
			intent.putExtra("DwarfID", dwarfId);
			context.startActivity(intent);
		}

		else if(game==2){
			Intent intent = new Intent(context,ARGameActivity.class);
			intent.putExtra("DwarfID", dwarfId);
			context.startActivity(intent);
		}


	}

	public static void setDwarfWaitingForPlay(Dwarf dwarf){
		DwarfAccess.setDwarfGameScore(dwarf, false, true, false);
	}

	public static void setDwarfUnlocked(Dwarf dwarf){
		DwarfAccess.setDwarfGameScore(dwarf, true, false, false);
	}

	public static void getMoreDwarfsVisible(){

		List<DwarfDescription> listOfVisible = DwarfAccess.getVisibleDwarvesDescriptions(true);

		int delta = ApplicationSharedPreferences.getNumberOfDwarfs() - listOfVisible.size();

		if(delta > 0)
			DwarfAccess.setNRandomDwarfAsVisible(delta);

	}
	

	public static void handleBonusActivation(Context context, int bonusDescriptionId, Location location) {
		BonusTypeDescription typeDescr = BonusAccess.getBonusTypeDescriptionForBonusDescription(BonusAccess.getBonusDescriptionById(bonusDescriptionId));
		int bonusType = typeDescr.getBonusType().getBonusType();
		switch(bonusType) {
			case BONUS_TYPE_PLUS_ONE_DWARF:
				ApplicationSharedPreferences.incrementNumberOfDwarves();
				getMoreDwarfsVisible();
				break;
			case BONUS_TYPE_MINUS_ONE_DWARF:
				ApplicationSharedPreferences.decrementNumberOfDwarves();
				break;
			case BONUS_TYPE_PLUS_N_DWARVES_ONCE:
				int max = context.getResources().getInteger(R.integer.bonus_plus_n_dwarfs_max);
				DwarfAccess.setNRandomDwarfAsVisible(new Random().nextInt(max)+1);
				break;
			case BONUS_TYPE_MINUS_N_DWARVES_ONCE:
				max = context.getResources().getInteger(R.integer.bonus_minus_n_dwarfs_max);
				DwarfAccess.setNRandomDwarfsAsInvisible(new Random().nextInt(max)+1);
				break;
			case BONUS_TYPE_PLUS_CLOSEST_DWARF:
				List<DwarfDescription> dwarfs = DwarfAccess.getLockedAndInvisibleDwarfsDescription();
				if(dwarfs.size() > 0) {
					DwarfDescription closestDwarf = null;
					double minDistance = Double.MAX_VALUE;
					for(DwarfDescription dwarf : dwarfs) {
						Location dwarfLocation = new Location("FAKE");
						dwarfLocation.setLatitude(dwarf.getDwarf().getLatitude());
						dwarfLocation.setLongitude(dwarf.getDwarf().getLongitude());
						double distance = location.distanceTo(dwarfLocation);
						if(closestDwarf == null || distance < minDistance) {
							closestDwarf = dwarf;
							minDistance = distance;
						}
					}
					DwarfAccess.setDwarfGameScore(closestDwarf.getDwarf(), false, false, true);
				}
				break;
			default:
				break;
		}
	}

}
